<?php
namespace User\Controller;
use User\Controller\BaseController;
class IndexController extends BaseController {
	/*
     * 用户中心首页
     */
    public function index(){
        $id=$_SESSION["id"];
        $users=M("users")->where("id='$id'")->find();
        $users_detail=M("users_detail")->where("id='$id'")->find();
        $this->assign("users",$users);
        $this->assign("users_detail",$users_detail);
        $this->display();

    }
    /*
     *  注册
     */
    public function register(){
    	if($this->user_id > 0) header("Location: ".U('Home/User/index'));
        $this->display();
    }
    /*
     *  处理注册信息并入库
     */
    public function register_do(){
    	$data=I("post.");
    	$nickname=trim($data["nickname"]);
    	$code=trim($data["code"]);
        $code_match=$this->get_code($code);
    	$password=md5($data["password"]);
        dump($password);
        $M=M("users");
        $M->create();
        $data["reg_time"] = time();
        $data["password"] = $password;
        if($code_match == 1){
            $data["mobile"] = $code;
        }elseif($code_match == 2){
            $data["email"] = $code;
        }
        $M->add($data);
        $id=$M->field("id")->where("mobile='$code' or email='$code'")->find();
        $id=$id["id"];
        $M=M("users_detail");
        $data["id"] = $id;
        $M->add($data);
        redirect(U('User/Index/login'), 0, '页面跳转中...');
    }
    /*
     *  登录
     */
    public function login(){
    	if($this->user_id > 0){
        	header("Location: ".U('User/Index/index'));
        }           
        $this->display();
    }
    /*
     *  登录处理入库
     */
    public function login_do(){
        $code = $_POST['code'];
        $pwd = $_POST['pwd'];
        $password=md5($pwd);
        $get_code=$this->get_code($code);
        if($get_code==1){
            $M=M("users");
            $id=$M->field("id")->where("mobile='$code' and password='$password'")->find();
            if($id){
                $id=$id["id"];
                echo "true";
                $_SESSION["id"]=$id;
            }else{
                echo "false";
            }
        }elseif($get_code==2){
            $M=M("users");
            $id=$M->field("id")->where("email='$code' and password='$password'")->find();
            if($id){
                $id=$id["id"];
                echo "true";
                $_SESSION["id"]=$id;
            }else{
                echo "false";
            }
        }else{
         echo "false";
        }
        $M = M("users");
        $M->create();
        $M->last_login = time();
        $M->where("id='$id'")->save($data);
        $data=$M->where("id='$id'")->find();
        $_SESSION['nickname']=$data['nickname'];
        $_SESSION['id']=$id;
    }
    /*
     *  退出登录
     */
    public function logout(){
    	setcookie('uname','',time()-3600,'/');
    	setcookie('cn','',time()-3600,'/');
    	setcookie('user_id','',time()-3600,'/');
        session_unset();
        session_destroy();
        //$this->success("退出成功",U('Home/Index/index'));
        header("location:".U('Home/Index/index'));
        exit;
    }
    /*
     *  会员信息修改
     */
    public function edit_users(){
        $id=$_SESSION["id"];
        $M=M("users_detail");
        $M->create();
        $M->where("id ='$id'")->save($data);
        $M=M("users");
        $M->create();
        $M->where("id ='$id'")->save($data);
        redirect(U('User/Index/index'), 0, '页面跳转中...');
    }
    /*
     *  处理邮箱手机号码
     */
    public function get_code($code){
    	if(preg_match("/^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$/",$code)){
    		return "1"; //是手机号码
    	}else if(preg_match("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/",$code)){
    		return "2"; //是邮箱号码
    	}else{
    		return "0"; //错误号码
    	}
    }
    /*
     *  处理密码
     */
    public function get_pwd($pwd){
        if(preg_match("/^[a-zA-Z\d]{6,16}$/",$pwd)){
            return "1"; //是
        }else{
            return "0"; //错误
        }
    }
     /*
      *  ajax处理邮箱和手机号
      */
    public function code_match(){
        $code = $_POST['code'];
        $get_code=$this->get_code($code);
        if($get_code == 1){
            if(M("users")->where("mobile='$code'")->find()){
                echo "手机被注册";
            }else{
                echo "可以注册";
            }
        }elseif($get_code == 2){
            if(M("users")->where("email='$code'")->find()){
                echo "邮箱被注册";
            }else{
                echo "可以注册";
            }
        }elseif($get_code == 0){
            echo "输入格式错误";
        }
    }
     /*
      *  ajax处理密码长度
      */
    public function pwd_match(){
        $pwd = $_POST['pwd'];
        $get_pwd=$this->get_pwd($pwd);
        echo "$get_pwd";
    }
}